<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Class: Origami::Filter::CCITTFax</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Class</strong></td>
          <td class="class-name-in-header">Origami::Filter::CCITTFax</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../../files/origami/filters/ccitt_rb.html">
                origami/filters/ccitt.rb
                </a>
        <br />
            </td>
        </tr>

        <tr class="top-aligned-row">
            <td><strong>Parent:</strong></td>
            <td>
                Object
            </td>
        </tr>
        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">

    <div id="description">
      <p>
Class representing a <a href="../Filter.html">Filter</a> used to <a
href="CCITTFax.html#M000349">encode</a> and <a
href="CCITTFax.html#M000350">decode</a> data with CCITT-facsimile
compression algorithm.
</p>

    </div>


   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M000350">decode</a>&nbsp;&nbsp;
      <a href="#M000349">encode</a>&nbsp;&nbsp;
      <a href="#M000348">new</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->
    <div id="includes">
      <h3 class="section-bar">Included Modules</h3>

      <div id="includes-list">
        <span class="include-name"><a href="../Filter.html">Filter</a></span>
      </div>
    </div>

    <div id="section">

    <div id="class-list">
      <h3 class="section-bar">Classes and Modules</h3>

      Class <a href="CCITTFax/DecodeParms.html" class="link">Origami::Filter::CCITTFax::DecodeParms</a><br />

    </div>

    <div id="constants-list">
      <h3 class="section-bar">Constants</h3>

      <div class="name-list">
        <table summary="Constants">
        <tr class="top-aligned-row context-row">
          <td class="context-item-name">EOL</td>
          <td>=</td>
          <td class="context-item-value">codeword('000000000001')</td>
        </tr>
        <tr class="top-aligned-row context-row">
          <td class="context-item-name">RTC</td>
          <td>=</td>
          <td class="context-item-value">codeword('000000000001' * 6)</td>
        </tr>
        <tr class="top-aligned-row context-row">
          <td class="context-item-name">WHITE_TERMINAL_ENCODE_TABLE</td>
          <td>=</td>
          <td class="context-item-value">{         0   =&gt; codeword('00110101'),          1   =&gt; codeword('000111'),         2   =&gt; codeword('0111'),         3   =&gt; codeword('1000'),         4   =&gt; codeword('1011'),         5   =&gt; codeword('1100'),         6   =&gt; codeword('1110'),         7   =&gt; codeword('1111'),         8   =&gt; codeword('10011'),         9   =&gt; codeword('10100'),         10  =&gt; codeword('00111'),         11  =&gt; codeword('01000'),         12  =&gt; codeword('001000'),         13  =&gt; codeword('000011'),         14  =&gt; codeword('110100'),         15  =&gt; codeword('110101'),         16  =&gt; codeword('101010'),         17  =&gt; codeword('101011'),         18  =&gt; codeword('0100111'),         19  =&gt; codeword('0001100'),         20  =&gt; codeword('0001000'),         21  =&gt; codeword('0010111'),         22  =&gt; codeword('0000011'),         23  =&gt; codeword('0000100'),         24  =&gt; codeword('0101000'),         25  =&gt; codeword('0101011'),         26  =&gt; codeword('0010011'),         27  =&gt; codeword('0100100'),         28  =&gt; codeword('0011000'),         29  =&gt; codeword('00000010'),         30  =&gt; codeword('00000011'),         31  =&gt; codeword('00011010'),         32  =&gt; codeword('00011011'),         33  =&gt; codeword('00010010'),         34  =&gt; codeword('00010011'),         35  =&gt; codeword('00010100'),         36  =&gt; codeword('00010101'),         37  =&gt; codeword('00010110'),         38  =&gt; codeword('00010111'),         39  =&gt; codeword('00101000'),         40  =&gt; codeword('00101001'),         41  =&gt; codeword('00101010'),         42  =&gt; codeword('00101011'),         43  =&gt; codeword('00101100'),         44  =&gt; codeword('00101101'),         45  =&gt; codeword('00000100'),         46  =&gt; codeword('00000101'),         47  =&gt; codeword('00001010'),         48  =&gt; codeword('00001011'),         49  =&gt; codeword('01010010'),         50  =&gt; codeword('01010011'),         51  =&gt; codeword('01010100'),         52  =&gt; codeword('01010101'),         53  =&gt; codeword('00100100'),         54  =&gt; codeword('00100101'),         55  =&gt; codeword('01011000'),         56  =&gt; codeword('01011001'),         57  =&gt; codeword('01011010'),         58  =&gt; codeword('01011011'),         59  =&gt; codeword('01001010'),         60  =&gt; codeword('01001011'),         61  =&gt; codeword('00110010'),         62  =&gt; codeword('00110011'),         63  =&gt; codeword('00110100')</td>
        </tr>
        <tr class="top-aligned-row context-row">
          <td class="context-item-name">WHITE_TERMINAL_DECODE_TABLE</td>
          <td>=</td>
          <td class="context-item-value">WHITE_TERMINAL_ENCODE_TABLE.invert</td>
        </tr>
        <tr class="top-aligned-row context-row">
          <td class="context-item-name">BLACK_TERMINAL_ENCODE_TABLE</td>
          <td>=</td>
          <td class="context-item-value">{         0   =&gt; codeword('0000110111'),          1   =&gt; codeword('010'),         2   =&gt; codeword('11'),         3   =&gt; codeword('10'),         4   =&gt; codeword('011'),         5   =&gt; codeword('0011'),         6   =&gt; codeword('0010'),         7   =&gt; codeword('00011'),         8   =&gt; codeword('000101'),         9   =&gt; codeword('000100'),         10  =&gt; codeword('0000100'),         11  =&gt; codeword('0000101'),         12  =&gt; codeword('0000111'),         13  =&gt; codeword('00000100'),         14  =&gt; codeword('00000111'),         15  =&gt; codeword('000011000'),         16  =&gt; codeword('0000010111'),         17  =&gt; codeword('0000011000'),         18  =&gt; codeword('0000001000'),         19  =&gt; codeword('00001100111'),         20  =&gt; codeword('00001101000'),         21  =&gt; codeword('00001101100'),         22  =&gt; codeword('00000110111'),         23  =&gt; codeword('00000101000'),         24  =&gt; codeword('00000010111'),         25  =&gt; codeword('00000011000'),         26  =&gt; codeword('000011001010'),         27  =&gt; codeword('000011001011'),         28  =&gt; codeword('000011001100'),         29  =&gt; codeword('000011001101'),         30  =&gt; codeword('000001101000'),         31  =&gt; codeword('000001101001'),         32  =&gt; codeword('000001101010'),         33  =&gt; codeword('000001101011'),         34  =&gt; codeword('000011010010'),         35  =&gt; codeword('000011010011'),         36  =&gt; codeword('000011010100'),         37  =&gt; codeword('000011010101'),         38  =&gt; codeword('000011010110'),         39  =&gt; codeword('000011010111'),         40  =&gt; codeword('000001101100'),         41  =&gt; codeword('000001101101'),         42  =&gt; codeword('000011011010'),         43  =&gt; codeword('000011011011'),         44  =&gt; codeword('000001010100'),         45  =&gt; codeword('000001010101'),         46  =&gt; codeword('000001010110'),         47  =&gt; codeword('000001010111'),         48  =&gt; codeword('000001100100'),         49  =&gt; codeword('000001100101'),         50  =&gt; codeword('000001010010'),         51  =&gt; codeword('000001010011'),         52  =&gt; codeword('000000100100'),         53  =&gt; codeword('000000110111'),         54  =&gt; codeword('000000111000'),         55  =&gt; codeword('000000100111'),         56  =&gt; codeword('000000101000'),         57  =&gt; codeword('000001011000'),         58  =&gt; codeword('000001011001'),         59  =&gt; codeword('000000101011'),         60  =&gt; codeword('000000101100'),         61  =&gt; codeword('000001011010'),         62  =&gt; codeword('000001100110'),         63  =&gt; codeword('000001100111')</td>
        </tr>
        <tr class="top-aligned-row context-row">
          <td class="context-item-name">BLACK_TERMINAL_DECODE_TABLE</td>
          <td>=</td>
          <td class="context-item-value">BLACK_TERMINAL_ENCODE_TABLE.invert</td>
        </tr>
        <tr class="top-aligned-row context-row">
          <td class="context-item-name">WHITE_CONFIGURATION_ENCODE_TABLE</td>
          <td>=</td>
          <td class="context-item-value">{         64    =&gt; codeword('11011'),         128   =&gt; codeword('10010'),         192   =&gt; codeword('010111'),         256   =&gt; codeword('0110111'),         320   =&gt; codeword('00110110'),         384   =&gt; codeword('00110111'),         448   =&gt; codeword('01100100'),         512   =&gt; codeword('01100101'),         576   =&gt; codeword('01101000'),         640   =&gt; codeword('01100111'),         704   =&gt; codeword('011001100'),         768   =&gt; codeword('011001101'),         832   =&gt; codeword('011010010'),         896   =&gt; codeword('011010011'),         960   =&gt; codeword('011010100'),         1024  =&gt; codeword('011010101'),         1088  =&gt; codeword('011010110'),         1152  =&gt; codeword('011010111'),         1216  =&gt; codeword('011011000'),         1280  =&gt; codeword('011011001'),         1344  =&gt; codeword('011011010'),         1408  =&gt; codeword('011011011'),         1472  =&gt; codeword('010011000'),         1536  =&gt; codeword('010011001'),         1600  =&gt; codeword('010011010'),         1664  =&gt; codeword('011000'),         1728  =&gt; codeword('010011011'),          1792  =&gt; codeword('00000001000'),         1856  =&gt; codeword('00000001100'),         1920  =&gt; codeword('00000001001'),         1984  =&gt; codeword('000000010010'),         2048  =&gt; codeword('000000010011'),         2112  =&gt; codeword('000000010100'),         2176  =&gt; codeword('000000010101'),         2240  =&gt; codeword('000000010110'),         2340  =&gt; codeword('000000010111'),         2368  =&gt; codeword('000000011100'),         2432  =&gt; codeword('000000011101'),         2496  =&gt; codeword('000000011110'),         2560  =&gt; codeword('000000011111')</td>
        </tr>
        <tr class="top-aligned-row context-row">
          <td class="context-item-name">WHITE_CONFIGURATION_DECODE_TABLE</td>
          <td>=</td>
          <td class="context-item-value">WHITE_CONFIGURATION_ENCODE_TABLE.invert</td>
        </tr>
        <tr class="top-aligned-row context-row">
          <td class="context-item-name">BLACK_CONFIGURATION_ENCODE_TABLE</td>
          <td>=</td>
          <td class="context-item-value">{         64    =&gt; codeword('0000001111'),         128   =&gt; codeword('000011001000'),         192   =&gt; codeword('000011001001'),         256   =&gt; codeword('000001011011'),         320   =&gt; codeword('000000110011'),         384   =&gt; codeword('000000110100'),         448   =&gt; codeword('000000110101'),         512   =&gt; codeword('0000001101100'),         576   =&gt; codeword('0000001101101'),         640   =&gt; codeword('0000001001010'),         704   =&gt; codeword('0000001001011'),         768   =&gt; codeword('0000001001100'),         832   =&gt; codeword('0000001001101'),         896   =&gt; codeword('0000001110010'),         960   =&gt; codeword('0000001110011'),         1024  =&gt; codeword('0000001110100'),         1088  =&gt; codeword('0000001110101'),         1152  =&gt; codeword('0000001110110'),         1216  =&gt; codeword('0000001110111'),         1280  =&gt; codeword('0000001010010'),         1344  =&gt; codeword('0000001010011'),         1408  =&gt; codeword('0000001010100'),         1472  =&gt; codeword('0000001010101'),         1536  =&gt; codeword('0000001011010'),         1600  =&gt; codeword('0000001011011'),         1664  =&gt; codeword('0000001100100'),         1728  =&gt; codeword('0000001100101'),                  1792  =&gt; codeword('00000001000'),         1856  =&gt; codeword('00000001100'),         1920  =&gt; codeword('00000001001'),         1984  =&gt; codeword('000000010010'),         2048  =&gt; codeword('000000010011'),         2112  =&gt; codeword('000000010100'),         2176  =&gt; codeword('000000010101'),         2240  =&gt; codeword('000000010110'),         2340  =&gt; codeword('000000010111'),         2368  =&gt; codeword('000000011100'),         2432  =&gt; codeword('000000011101'),         2496  =&gt; codeword('000000011110'),         2560  =&gt; codeword('000000011111')</td>
        </tr>
        <tr class="top-aligned-row context-row">
          <td class="context-item-name">BLACK_CONFIGURATION_DECODE_TABLE</td>
          <td>=</td>
          <td class="context-item-value">BLACK_CONFIGURATION_ENCODE_TABLE.invert</td>
        </tr>
        </table>
      </div>
    </div>



      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Class methods</h3>

      <div id="method-M000348" class="method-detail">
        <a name="M000348"></a>

        <div class="method-heading">
          <a href="#M000348" class="method-signature">
          <span class="method-name">new</span><span class="method-args">(parameters = {})</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Creates a <a href="CCITTFax.html#M000348">new</a> CCITT Fax <a
href="../Filter.html">Filter</a>.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000348-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000348-source">
<pre>
     <span class="ruby-comment cmt"># File origami/filters/ccitt.rb, line 295</span>
295:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">initialize</span>(<span class="ruby-identifier">parameters</span> = {})
296:         <span class="ruby-keyword kw">super</span>(<span class="ruby-constant">DecodeParms</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">parameters</span>))  
297:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M000350" class="method-detail">
        <a name="M000350"></a>

        <div class="method-heading">
          <a href="#M000350" class="method-signature">
          <span class="method-name">decode</span><span class="method-args">(stream)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Decodes data using CCITT-facsimile compression method.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000350-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000350-source">
<pre>
     <span class="ruby-comment cmt"># File origami/filters/ccitt.rb, line 365</span>
365:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">decode</span>(<span class="ruby-identifier">stream</span>)
366:         
367:         <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@params</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">:K</span>) <span class="ruby-keyword kw">and</span> <span class="ruby-ivar">@params</span>.<span class="ruby-constant">K</span> <span class="ruby-operator">!=</span> <span class="ruby-value">0</span>
368:           <span class="ruby-identifier">raise</span> <span class="ruby-constant">NotImplementedError</span>, <span class="ruby-value str">&quot;CCITT encoding scheme not supported&quot;</span>
369:         <span class="ruby-keyword kw">end</span>
370: 
371:         <span class="ruby-identifier">columns</span> = <span class="ruby-ivar">@params</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">:Columns</span>) <span class="ruby-operator">?</span> <span class="ruby-ivar">@params</span>.<span class="ruby-constant">Columns</span>.<span class="ruby-identifier">value</span> <span class="ruby-operator">:</span> <span class="ruby-value">1728</span>
372:         <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">columns</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-operator">::</span><span class="ruby-constant">Integer</span>) <span class="ruby-keyword kw">and</span> <span class="ruby-identifier">columns</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">0</span> <span class="ruby-keyword kw">and</span> <span class="ruby-identifier">columns</span> <span class="ruby-operator">%</span> <span class="ruby-value">8</span> <span class="ruby-operator">==</span> <span class="ruby-value">0</span>
373:           <span class="ruby-identifier">raise</span> <span class="ruby-constant">CCITTFaxFilterError</span>, <span class="ruby-value str">&quot;Invalid value for parameter `Columns'&quot;</span>
374:         <span class="ruby-keyword kw">end</span>
375: 
376:         <span class="ruby-identifier">white</span>, <span class="ruby-identifier">black</span> = (<span class="ruby-ivar">@params</span>.<span class="ruby-constant">BlackIs1</span> <span class="ruby-operator">==</span> <span class="ruby-keyword kw">true</span>) <span class="ruby-operator">?</span> [<span class="ruby-value">0</span>,<span class="ruby-value">1</span>] <span class="ruby-operator">:</span> [<span class="ruby-value">1</span>,<span class="ruby-value">0</span>]
377:         <span class="ruby-identifier">aligned</span> = <span class="ruby-ivar">@params</span>.<span class="ruby-constant">EncodedByteAlign</span> <span class="ruby-operator">==</span> <span class="ruby-keyword kw">true</span>
378:         <span class="ruby-identifier">has_eob</span> = <span class="ruby-ivar">@params</span>.<span class="ruby-constant">EndOfBlock</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-keyword kw">or</span> <span class="ruby-ivar">@params</span>.<span class="ruby-constant">EndOfBlock</span> <span class="ruby-operator">==</span> <span class="ruby-keyword kw">true</span>
379:         <span class="ruby-identifier">has_eol</span> = <span class="ruby-ivar">@params</span>.<span class="ruby-constant">EndOfLine</span> <span class="ruby-operator">==</span> <span class="ruby-keyword kw">true</span>
380: 
381:         <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">has_eob</span>
382:           <span class="ruby-keyword kw">unless</span> <span class="ruby-ivar">@params</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">:Rows</span>) <span class="ruby-keyword kw">and</span> <span class="ruby-ivar">@params</span>.<span class="ruby-constant">Rows</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-operator">::</span><span class="ruby-constant">Integer</span>) <span class="ruby-keyword kw">and</span> <span class="ruby-ivar">@params</span>.<span class="ruby-constant">Rows</span>.<span class="ruby-identifier">value</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">0</span>
383:             <span class="ruby-identifier">raise</span> <span class="ruby-constant">CCITTFaxFilterError</span>, <span class="ruby-value str">&quot;Invalid value for parameter `Rows'&quot;</span>
384:           <span class="ruby-keyword kw">end</span>
385: 
386:           <span class="ruby-identifier">rows</span> = <span class="ruby-ivar">@params</span>.<span class="ruby-constant">Rows</span>.<span class="ruby-identifier">to_i</span>
387:         <span class="ruby-keyword kw">end</span>
388: 
389:         <span class="ruby-identifier">bitr</span> = <span class="ruby-constant">Utils</span><span class="ruby-operator">::</span><span class="ruby-constant">BitReader</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">stream</span>)
390:         <span class="ruby-identifier">bitw</span> = <span class="ruby-constant">Utils</span><span class="ruby-operator">::</span><span class="ruby-constant">BitWriter</span>.<span class="ruby-identifier">new</span>
391: 
392:         <span class="ruby-identifier">current_color</span> = <span class="ruby-identifier">white</span>
393:         <span class="ruby-keyword kw">until</span> <span class="ruby-identifier">bitr</span>.<span class="ruby-identifier">eod?</span> <span class="ruby-keyword kw">or</span> <span class="ruby-identifier">rows</span> <span class="ruby-operator">==</span> <span class="ruby-value">0</span>
394:           
395:           <span class="ruby-comment cmt"># realign the read line on a 8-bit boundary if required</span>
396:           <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">aligned</span> <span class="ruby-keyword kw">and</span> <span class="ruby-identifier">bitr</span>.<span class="ruby-identifier">pos</span> <span class="ruby-operator">%</span> <span class="ruby-value">8</span> <span class="ruby-operator">!=</span> <span class="ruby-value">0</span>
397:             <span class="ruby-identifier">bitr</span>.<span class="ruby-identifier">pos</span> <span class="ruby-operator">+=</span> <span class="ruby-value">8</span> <span class="ruby-operator">-</span> (<span class="ruby-identifier">bitr</span>.<span class="ruby-identifier">pos</span> <span class="ruby-operator">%</span> <span class="ruby-value">8</span>)          
398:           <span class="ruby-keyword kw">end</span>
399: 
400:           <span class="ruby-comment cmt"># received return-to-control code </span>
401:           <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">has_eob</span> <span class="ruby-keyword kw">and</span> <span class="ruby-identifier">bitr</span>.<span class="ruby-identifier">peek</span>(<span class="ruby-constant">RTC</span>[<span class="ruby-value">1</span>]) <span class="ruby-operator">==</span> <span class="ruby-constant">RTC</span>[<span class="ruby-value">0</span>]
402:             <span class="ruby-identifier">bitr</span>.<span class="ruby-identifier">pos</span> <span class="ruby-operator">+=</span> <span class="ruby-constant">RTC</span>[<span class="ruby-value">1</span>]
403:             <span class="ruby-keyword kw">break</span>
404:           <span class="ruby-keyword kw">end</span>
405: 
406:           <span class="ruby-comment cmt"># checking for the presence of EOL</span>
407:           <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">bitr</span>.<span class="ruby-identifier">peek</span>(<span class="ruby-constant">EOL</span>[<span class="ruby-value">1</span>]) <span class="ruby-operator">!=</span> <span class="ruby-constant">EOL</span>[<span class="ruby-value">0</span>]
408:             <span class="ruby-identifier">raise</span> <span class="ruby-constant">CCITTFaxFilterError</span>, <span class="ruby-node">&quot;No end-of-line pattern found (at bit pos #{bitr.pos}/#{bitr.size}})&quot;</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">has_eol</span>
409:           <span class="ruby-keyword kw">else</span>
410:             <span class="ruby-identifier">bitr</span>.<span class="ruby-identifier">pos</span> <span class="ruby-operator">+=</span> <span class="ruby-constant">EOL</span>[<span class="ruby-value">1</span>]
411:           <span class="ruby-keyword kw">end</span>
412: 
413:           <span class="ruby-identifier">line_length</span> = <span class="ruby-value">0</span>
414:           <span class="ruby-keyword kw">while</span> <span class="ruby-identifier">line_length</span> <span class="ruby-operator">&lt;</span> <span class="ruby-identifier">columns</span>
415:             <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">current_color</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">white</span>
416:               <span class="ruby-identifier">bit_length</span> = <span class="ruby-identifier">get_white_bits</span>(<span class="ruby-identifier">bitr</span>)
417:             <span class="ruby-keyword kw">else</span>
418:               <span class="ruby-identifier">bit_length</span> = <span class="ruby-identifier">get_black_bits</span>(<span class="ruby-identifier">bitr</span>)
419:             <span class="ruby-keyword kw">end</span>
420: 
421:             <span class="ruby-identifier">raise</span> <span class="ruby-constant">CCITTFaxFilterError</span>, <span class="ruby-node">&quot;Unfinished line (at bit pos #{bitr.pos}/#{bitr.size}})&quot;</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">bit_length</span>.<span class="ruby-identifier">nil?</span>
422:             
423:             <span class="ruby-identifier">line_length</span> <span class="ruby-operator">+=</span> <span class="ruby-identifier">bit_length</span>
424:             <span class="ruby-identifier">raise</span> <span class="ruby-constant">CCITTFaxFilterError</span>, <span class="ruby-node">&quot;Line is too long (at bit pos #{bitr.pos}/#{bitr.size}})&quot;</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">line_length</span> <span class="ruby-operator">&gt;</span> <span class="ruby-identifier">columns</span>
425: 
426:             <span class="ruby-identifier">write_bit_range</span>(<span class="ruby-identifier">bitw</span>, <span class="ruby-identifier">current_color</span>, <span class="ruby-identifier">bit_length</span>)
427:             <span class="ruby-identifier">current_color</span> <span class="ruby-operator">^=</span> <span class="ruby-value">1</span>
428:           <span class="ruby-keyword kw">end</span>
429: 
430:           <span class="ruby-identifier">rows</span> <span class="ruby-operator">-=</span> <span class="ruby-value">1</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">has_eob</span>
431:         <span class="ruby-keyword kw">end</span>
432: 
433:         <span class="ruby-identifier">bitw</span>.<span class="ruby-identifier">final</span>.<span class="ruby-identifier">to_s</span>
434:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000349" class="method-detail">
        <a name="M000349"></a>

        <div class="method-heading">
          <a href="#M000349" class="method-signature">
          <span class="method-name">encode</span><span class="method-args">(stream)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Encodes data using CCITT-facsimile compression method.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000349-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000349-source">
<pre>
     <span class="ruby-comment cmt"># File origami/filters/ccitt.rb, line 302</span>
302:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">encode</span>(<span class="ruby-identifier">stream</span>)
303:       
304:         <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@params</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">:K</span>) <span class="ruby-keyword kw">and</span> <span class="ruby-ivar">@params</span>.<span class="ruby-constant">K</span> <span class="ruby-operator">!=</span> <span class="ruby-value">0</span>
305:           <span class="ruby-identifier">raise</span> <span class="ruby-constant">NotImplementedError</span>, <span class="ruby-value str">&quot;CCITT encoding scheme not supported&quot;</span>
306:         <span class="ruby-keyword kw">end</span>
307: 
308:         <span class="ruby-identifier">columns</span> = <span class="ruby-ivar">@params</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">:Columns</span>) <span class="ruby-operator">?</span> <span class="ruby-ivar">@params</span>.<span class="ruby-constant">Columns</span>.<span class="ruby-identifier">value</span> <span class="ruby-operator">:</span> (<span class="ruby-identifier">stream</span>.<span class="ruby-identifier">size</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-value">3</span>)
309:         <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">columns</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-operator">::</span><span class="ruby-constant">Integer</span>) <span class="ruby-keyword kw">and</span> <span class="ruby-identifier">columns</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">0</span> <span class="ruby-keyword kw">and</span> <span class="ruby-identifier">columns</span> <span class="ruby-operator">%</span> <span class="ruby-value">8</span> <span class="ruby-operator">==</span> <span class="ruby-value">0</span>
310:           <span class="ruby-identifier">raise</span> <span class="ruby-constant">CCITTFaxFilterError</span>, <span class="ruby-value str">&quot;Invalid value for parameter `Columns'&quot;</span>
311:         <span class="ruby-keyword kw">end</span>
312: 
313:         <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">stream</span>.<span class="ruby-identifier">size</span> <span class="ruby-operator">%</span> (<span class="ruby-identifier">columns</span> <span class="ruby-operator">&gt;&gt;</span> <span class="ruby-value">3</span>) <span class="ruby-operator">!=</span> <span class="ruby-value">0</span>
314:           <span class="ruby-identifier">raise</span> <span class="ruby-constant">CCITTFaxFilterError</span>, <span class="ruby-value str">&quot;Data size is not a multiple of image width&quot;</span>
315:         <span class="ruby-keyword kw">end</span>
316: 
317:         <span class="ruby-identifier">white</span>, <span class="ruby-identifier">black</span> = (<span class="ruby-ivar">@params</span>.<span class="ruby-constant">BlackIs1</span> <span class="ruby-operator">==</span> <span class="ruby-keyword kw">true</span>) <span class="ruby-operator">?</span> [<span class="ruby-value">0</span>,<span class="ruby-value">1</span>] <span class="ruby-operator">:</span> [<span class="ruby-value">1</span>,<span class="ruby-value">0</span>]
318: 
319:         <span class="ruby-identifier">bitr</span> = <span class="ruby-constant">Utils</span><span class="ruby-operator">::</span><span class="ruby-constant">BitReader</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">stream</span>)
320:         <span class="ruby-identifier">bitw</span> = <span class="ruby-constant">Utils</span><span class="ruby-operator">::</span><span class="ruby-constant">BitWriter</span>.<span class="ruby-identifier">new</span>
321: 
322:         <span class="ruby-keyword kw">until</span> <span class="ruby-identifier">bitr</span>.<span class="ruby-identifier">eod?</span>
323:           
324:           <span class="ruby-identifier">bitw</span>.<span class="ruby-identifier">write</span>(<span class="ruby-operator">*</span><span class="ruby-constant">EOL</span>)
325:           <span class="ruby-identifier">scan_len</span> = <span class="ruby-value">0</span>
326:           <span class="ruby-identifier">current_color</span> = <span class="ruby-identifier">white</span>
327: 
328:           <span class="ruby-comment cmt"># Process each bit in line.</span>
329:           <span class="ruby-keyword kw">begin</span>
330:             <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">bitr</span>.<span class="ruby-identifier">read</span>(<span class="ruby-value">1</span>) <span class="ruby-operator">==</span> <span class="ruby-identifier">current_color</span>
331:               <span class="ruby-identifier">scan_len</span> <span class="ruby-operator">+=</span> <span class="ruby-value">1</span>
332:             <span class="ruby-keyword kw">else</span>
333:               <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">current_color</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">white</span>
334:                 <span class="ruby-identifier">put_white_bits</span>(<span class="ruby-identifier">bitw</span>, <span class="ruby-identifier">scan_len</span>)
335:               <span class="ruby-keyword kw">else</span>
336:                 <span class="ruby-identifier">put_black_bits</span>(<span class="ruby-identifier">bitw</span>, <span class="ruby-identifier">scan_len</span>)
337:               <span class="ruby-keyword kw">end</span>
338: 
339:               <span class="ruby-identifier">current_color</span> <span class="ruby-operator">^=</span> <span class="ruby-value">1</span>
340:               <span class="ruby-identifier">scan_len</span> = <span class="ruby-value">1</span>
341:             <span class="ruby-keyword kw">end</span>
342:           <span class="ruby-keyword kw">end</span> <span class="ruby-keyword kw">while</span> <span class="ruby-identifier">bitr</span>.<span class="ruby-identifier">pos</span> <span class="ruby-operator">%</span> <span class="ruby-identifier">columns</span> <span class="ruby-operator">!=</span> <span class="ruby-value">0</span>
343: 
344:           <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">current_color</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">white</span>
345:             <span class="ruby-identifier">put_white_bits</span>(<span class="ruby-identifier">bitw</span>, <span class="ruby-identifier">scan_len</span>)
346:           <span class="ruby-keyword kw">else</span>
347:             <span class="ruby-identifier">put_black_bits</span>(<span class="ruby-identifier">bitw</span>, <span class="ruby-identifier">scan_len</span>)
348:           <span class="ruby-keyword kw">end</span>
349: 
350:           <span class="ruby-comment cmt"># Align encoded lign on a 8-bit boundary.</span>
351:           <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@params</span>.<span class="ruby-constant">EncodedByteAlign</span> <span class="ruby-operator">==</span> <span class="ruby-keyword kw">true</span> <span class="ruby-keyword kw">and</span> <span class="ruby-identifier">bitw</span>.<span class="ruby-identifier">pos</span> <span class="ruby-operator">%</span> <span class="ruby-value">8</span> <span class="ruby-operator">!=</span> <span class="ruby-value">0</span>
352:             <span class="ruby-identifier">bitw</span>.<span class="ruby-identifier">write</span>(<span class="ruby-value">0</span>, <span class="ruby-value">8</span> <span class="ruby-operator">-</span> (<span class="ruby-identifier">bitw</span>.<span class="ruby-identifier">pos</span> <span class="ruby-operator">%</span> <span class="ruby-value">8</span>))
353:           <span class="ruby-keyword kw">end</span>
354:         <span class="ruby-keyword kw">end</span>
355: 
356:         <span class="ruby-comment cmt"># Emit return-to-control code</span>
357:         <span class="ruby-identifier">bitw</span>.<span class="ruby-identifier">write</span>(<span class="ruby-operator">*</span><span class="ruby-constant">RTC</span>)
358: 
359:         <span class="ruby-identifier">bitw</span>.<span class="ruby-identifier">final</span>.<span class="ruby-identifier">to_s</span>
360:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>